﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System;
using System.Xml.Linq;

namespace Chat_WebServer.Controllers
{
    public class AccountController : ApiController
    {
        // Danh sach nguoi dung dang online
        public static List<string> onlines = new List<string>();

        // Dang nhap
        public Account Get([FromUri]string username, [FromUri]string pass)
        {
            using (var db = new AccountEntities())
            {
                try
                {
                    // neu chua dang nhap
                    if (onlines.IndexOf(username) < 0)
                    {
                        var temp = db.Accounts.ToList().FirstOrDefault(a => a.Username.Trim() == username && ((a.Password != null && a.Password.Trim() == pass) || (a.OpenID != null && a.OpenID.Trim() == pass)));
                        if (temp != null)
                        {
                            onlines.Add(username);
                            return temp;
                        }
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }
        }

        // Dang ky
        public string Post(Account acc)
        {
            using (var db = new AccountEntities())
            {
                try
                {
                    if (db.Accounts.ToList().Where(a => a.Username.Trim() == acc.Username).Count() == 0)
                    {
                        // them vao bang account
                        db.AddToAccounts(acc);

                        // them vao bang friend
                        Friend friend = new Friend();
                        friend.UserName = acc.Username;
                        friend.Friends = "<friends></friends>";
                        db.AddToFriends(friend);

                        // them vao bang message
                        Message mess = new Message();
                        mess.UserName = acc.Username;
                        mess.Messages = "<messages></messages>";
                        db.AddToMessages(mess);

                        // save database
                        db.SaveChanges();

                        // them vao danh sach dang online
                        onlines.Add(acc.Username);

                        return acc.Username;
                    }
                    return null;
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }
        }

        public Account GetCheckUser([FromUri]String ClaimID, [FromUri]String Email)
        {
            try
            {
                using (var db = new AccountEntities())
                {
                    Account user = db.Accounts.FirstOrDefault(u => (u.OpenID != null && u.OpenID == ClaimID.Trim()));
                    //user is exists
                    if (user != null)
                        return user;
                    else
                    {
                        Account acc = new Account();
                        acc.OpenID = ClaimID;
                        acc.Username = Email;
                        db.Accounts.AddObject(acc);
                        db.SaveChanges();
                        return acc;
                    }

                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        // Thoat ra
        public void Delete(string username)
        {
            using (var db = new AccountEntities())
            {
                onlines.Remove(username);
            }
        }
    }
}
